package com.zwz.controller;

import java.beans.PropertyEditorSupport;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.InitBinder;

import com.alibaba.fastjson.JSON;
import com.sun.tools.internal.xjc.reader.xmlschema.bindinfo.BIConversion.User;
import com.zwz.bean.ShiroUser;
import com.zwz.service.baseConfig.SysUserService;
import com.zwz.service.staticConfig.RoleAuthService;

/**
 * @description：基础 controller
 * @author：zhixuan.wang
 * @date：2015/10/1 14:51
 */
public class BaseController {

    @Autowired
    private SysUserService sysUserService;
    
	public final static String SUCCESS ="code";  
	public final static String MSG ="errorMsg";  

    /**
     * 获取当前登录用户对象
     * @return
     */
    public User getCurrentUser() {
//        ShiroUser user = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
//        User currentUser = userService.findUserById(user.id);
//        return currentUser;
    	return null;
    }

    /**
     * 获取当前登录用户id
     * @return
     */
    public Long getUserId() {
//        return this.getCurrentUser().getId();
    	return 0L;
    }

    /**
     * 获取当前登录用户名
     * @return
     */
    public String getStaffName() {
//        return this.getCurrentUser().getName();
    	return "";
    }
	/**
	 * 初始化数据绑定 1. 将所有传递进来的String进行HTML编码，防止XSS攻击 2. 将字段中Date类型转换为String类型
	 */
	@InitBinder
	protected void initBinder(WebDataBinder binder) {
		// String类型转换，将所有传递进来的String进行HTML编码，防止XSS攻击
		binder.registerCustomEditor(String.class, new PropertyEditorSupport() {
			@Override
			public void setAsText(String text) {
				// setValue(text == null ? null :
				// StringEscapeUtils.escapeHtml(text.trim()));
				setValue(text == null ? null : text.trim());
			}

			@Override
			public String getAsText() {
				Object value = getValue();
				return value != null ? value.toString() : "";
			}
		});
	}

	@ExceptionHandler(Exception.class)
	public String handleException(Exception ex, HttpServletRequest request, HttpServletResponse response)throws Exception {
		String type=request.getHeader("X-Requested-With");
		//AJAX
		if("XMLHttpRequest".equals(type)){
			Map<String, Object> map=sendFailureMessage("服务器异常,请联系管理员!");
			response.getWriter().write(JSON.toJSONString(map));
			return null;
		}else{
			request.setAttribute("exMsg", ex.getMessage());
			return "errors/exception";
		}
	}
	
	/**
	 * 提示成功信息
	 * @param message
	 *
	 */
	public Map<String, Object> sendSuccessMessage(String message) {
		Map<String, Object> result = new HashMap<String, Object>();
		result.put(SUCCESS, 0);
		result.put(MSG, message);
		return result;
	}

	/**
	 * 提示失败信息
	 * @param message
	 */
	public Map<String, Object> sendFailureMessage(String message) {
		Map<String, Object> result = new HashMap<String, Object>();
		result.put(SUCCESS, 1);
		result.put(MSG, message);
		return result;
	}
	
	/**
	 * 获得该角色下URL
	 * @return
	 */
	public List<String> getRoleAuth(){
		Subject user = SecurityUtils.getSubject();
		ShiroUser shiroUser = (ShiroUser)user.getPrincipal();
		List<String> roleAuth = RoleAuthService.getRoleAuth(shiroUser.getRoleCode());
		return roleAuth;
	}
}
